package com.teach.consumer;

/**
 * 注释 :  lenovo
 *
 * @author 罗帅哲
 * @version 1.0.0
 * @ClassName DeadOrderConsumer.java  / educate
 * @Description TODO
 * @createTime 2022年10月04日 11:25创建
 */

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.rabbitmq.client.Channel;
import com.teach.config.RabbitmqConfig;
import com.teach.entity.TbOrder;
import com.teach.mapper.TbOrderMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
public class DeadOrderConsumer {
    @Resource
    TbOrderMapper tbOrderMapper;

    @RabbitListener(queues = RabbitmqConfig.DELAY_ORDER_QUEUE)
    public void receiveOrderMessage(Message message, Channel channel, Integer id) throws IOException {//消费者
        //获取订单状态
         TbOrder order = tbOrderMapper.selectById(id);
         if(order.getPayState()==2){
             System.err.println(order.getName()+"：这个订单在规定时间里面已经完成支付");
             return;
         }

         LambdaUpdateWrapper<TbOrder> set = new LambdaUpdateWrapper<TbOrder>()
                .eq(TbOrder::getId, id)
                .set(TbOrder::getPayState, 3);
         tbOrderMapper.update(null,set);//修改支付状态为未支付

        System.err.println("消费未支付的订单："+order.getName());

        //签收机制
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}